import React from 'react';
import Taro, {getCurrentInstance} from '@tarojs/taro';
import {connect} from 'react-redux';
import * as T from './types';
import actions from './actions';
import {store2Props} from './selectors';
import Add from '@/assets/image/connections/add.png';
import {View, Image} from '@tarojs/components';
import './index.less';
import Search from './components/Search';
import FilterDropdown from './components/FilterDropdown';
import ListPage from './components/ListPage';
actions().actions.loadReducer();
import api from 'api';
// import dropdown from '@/utils/dropdown.js';//筛选菜单数据
// import address from '@/utils/address.js';//三级联动地址数据
// @ts-ignore
@connect<Partial<T.IProps>, any>(store2Props, actions)
export default class SupplyAndDemandA extends React.Component<Partial<T.IProps>, any> {
  // 使用state值
  constructor(props) {
    super(props);
    this.state = {};
  }

  async componentWillMount() {
    let {cat0Id, cat1Id, title} = getCurrentInstance().router.params;
    Taro.setNavigationBarTitle({title});
    let {
      main: {filterData},
      actions: {action},
    } = this.props;
    //省
    const res = await api.MeituanController.getMeituanProvince();
    if (res.code == 'K-000000') {
      let arr = this.convertToArray(res.context);
      filterData[0].children = arr;
      let key, code;
      arr.map((item, index) => {
        if (item.name == '江苏') {
          key = index;
          code = item.code;
        }
      });
      action.findMeituanCities(key, code); //默认江苏
    }
    action.commonChange([
      {paths: 'main.filterData', value: filterData},
      {paths: 'main.cat0Id', value: cat0Id},
      {paths: 'main.cat1Id', value: cat1Id},
    ]);
  }

  componentDidShow() {}

  componentWillUnmount() {
    this.props.actions.clean();
  }

  convertToArray(objct) {
    return Object.keys(objct).map((key) => ({code: key, name: objct[key], value: objct[key]}));
  }

  //接收菜单结果
  confirm = (e) => {
    console.log('------------------------e');
    console.log(e);
  };

  render() {
    if (!this.props.main) return null;
    let {
      main: {filterData, filterDropdownValue},
    } = this.props;
    return (
      <View className="packageDmeituan">
        <Search />
        <FilterDropdown
          filterData={filterData}
          defaultSelected={filterDropdownValue}
          updateMenuName
          confirm={this.confirm}
          dataFormat="Object"
        />
        <ListPage />
      </View>
    );
  }
}
